﻿ using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.ServiceModel;
using System.Xml;
//using Klinkby.MicroBus;
using log4net;

namespace Klinkby.MicroBus.Service
{
    public partial class MicrobusService : ServiceBase
    {
        readonly static ILog _log = LogManager.GetLogger(typeof(MicrobusService));
        IDisposable _receivers;
        //        readonly string _path = ;//Path.GetDirectoryName(typeof(Service1).Assembly.Location);

        public MicrobusService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            OnStop();
            _log.Info("Starting service");
            using (var rd = XmlReader.Create(
                Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.xml")
                ))
            {
                _receivers = Factory.Create(rd);
            }            
        }

        protected override void OnStop()
        {
            IDisposable receivers = _receivers;
            if (receivers != null)
            {
                _log.Info("Stopping service");
                _receivers = null;
                receivers.Dispose();
            }
        }
    }
}

